import xlrd
from selenium import webdriver
import time

from xlutils.copy import copy

import DBUtils

from selenium.webdriver.common.by import By


class Test_register:
    book = copy(xlrd.open_workbook(filename="a.xls", encoding_override=True))
    time = time.strftime('%y年%m月%d日%H:%M:%S')

    def test_register_success01(self):  # 正常用户注册
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")

        element01 = driver.find_element(By.XPATH, '//a[@href="/toregister.html"]')
        element01.click()
        element02 = driver.find_element(By.XPATH, '//input[@type="text" and @placeholder="请输入您的用户名"]')
        element02.send_keys("zhaoliu")
        element03 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请输入密码"]')
        element03.send_keys("123456")
        element04 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请重复密码"]')
        element04.send_keys("123456")
        element05 = driver.find_element(By.XPATH, '//button[@id="login_btn" and @class="btn btn-block btn-primary"]')
        element05.click()
        sql = "select * from user where username=%s"
        param = ["zhaoliu"]
        if len(DBUtils.select(sql, param)) == 1:
            print("注册成功")
            self.book.get_sheet(2).write(2, 6, "passed")
            self.book.get_sheet(2).write(2, 7, "晏凯旋")
            self.book.get_sheet(2).write(2, 8, self.time)
            self.book.save("b.xls")
        else:
            print("注册失败")
            driver.save_screenshot("zhaoliu--123456.jpg")
            self.book.get_sheet(2).write(2, 6, "failed")
            self.book.get_sheet(2).write(2, 7, "晏凯旋")
            self.book.get_sheet(2).write(2, 8, self.time)
            self.book.save("b.xls")
        driver.quit()
        assert len(DBUtils.select(sql, param)) == 1

    def test_register_failed02(self):  # 已存在用户 注册
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")

        element01 = driver.find_element(By.XPATH, '//a[@href="/toregister.html"]')
        element01.click()
        element02 = driver.find_element(By.XPATH, '//input[@type="text" and @placeholder="请输入您的用户名"]')
        element02.send_keys("wangwu")
        element03 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请输入密码"]')
        element03.send_keys("123456")
        element04 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请重复密码"]')
        element04.send_keys("123456")
        element05 = driver.find_element(By.XPATH, '//button[@id="login_btn" and @class="btn btn-block btn-primary"]')
        element05.click()
        sql = "select * from user where username=%s"
        param = ["wangwu"]
        if len(DBUtils.select(sql, param)) == 1:
            print("注册失败")
            self.book.get_sheet(2).write(4, 6, "passed")
            self.book.get_sheet(2).write(4, 7, "晏凯旋")
            self.book.get_sheet(2).write(4, 8, self.time)
            self.book.save("b.xls")
        else:
            print("注册成功")
            driver.save_screenshot("wangwu--123456.jpg")
            self.book.get_sheet(2).write(4, 5, "failed")
            self.book.get_sheet(2).write(4, 7, "晏凯旋")
            self.book.get_sheet(2).write(4, 8, self.time)
            self.book.save("b.xls")
        driver.quit()
        assert len(DBUtils.select(sql, param)) == 1

    def test_register_failed03(self):  # 正常注册 但2次密码输入不一致
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")

        element01 = driver.find_element(By.XPATH, '//a[@href="/toregister.html"]')
        element01.click()
        element02 = driver.find_element(By.XPATH, '//input[@type="text" and @placeholder="请输入您的用户名"]')
        element02.send_keys("zhaoliu001")
        element03 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请输入密码"]')
        element03.send_keys("123456")
        element04 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请重复密码"]')
        element04.send_keys("123458")
        element05 = driver.find_element(By.XPATH, '//button[@id="login_btn" and @class="btn btn-block btn-primary"]')
        element05.click()
        sql = "select * from user where username=%s"
        param = ["zhaoliu001"]
        if len(DBUtils.select(sql, param)) == 0:
            print("注册失败")
            self.book.get_sheet(2).write(5, 6, "passed")
            self.book.get_sheet(2).write(5, 7, "晏凯旋")
            self.book.get_sheet(2).write(5, 8, self.time)
            self.book.save("b.xls")
        else:
            print("注册成功")
            driver.save_screenshot("zhaoliu001--123456.jpg")
            self.book.get_sheet(2).write(5, 5, "failed")
            self.book.get_sheet(2).write(5, 7, "晏凯旋")
            self.book.get_sheet(2).write(5, 8, self.time)
            self.book.save("b.xls")
        driver.quit()
        assert len(DBUtils.select(sql, param)) == 0

    def test_register_failed04(self):  # 缺少确认密码注册
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")

        element01 = driver.find_element(By.XPATH, '//a[@href="/toregister.html"]')
        element01.click()
        element02 = driver.find_element(By.XPATH, '//input[@type="text" and @placeholder="请输入您的用户名"]')
        element02.send_keys("zhaoliu002")
        element03 = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请输入密码"]')
        element03.send_keys("123456")

        element05 = driver.find_element(By.XPATH, '//button[@id="login_btn" and @class="btn btn-block btn-primary"]')
        element05.click()
        sql = "select * from user where username=%s"
        param = ["zhaoliu002"]
        if len(DBUtils.select(sql, param)) == 0:
            print("注册失败")
            self.book.get_sheet(2).write(6, 6, "passed")
            self.book.get_sheet(2).write(6, 7, "晏凯旋")
            self.book.get_sheet(2).write(6, 8, self.time)
            self.book.save("b.xls")
        else:
            print("注册成功")
            driver.save_screenshot("zhaoliu002--123456.jpg")
            self.book.get_sheet(2).write(6, 5, "failed")
            self.book.get_sheet(2).write(6, 7, "晏凯旋")
            self.book.get_sheet(2).write(6, 8, self.time)
            self.book.save("b.xls")
        driver.quit()
        assert len(DBUtils.select(sql, param)) == 0

    def test_register_failed05(self):  # 缺少确认密码注册
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        element01 = driver.find_element(By.XPATH, '//a[@href="/toregister.html"]')
        element01.click()
        element02 = driver.find_element(By.XPATH, '//input[@type="text" and @placeholder="请输入您的用户名"]')
        element02.send_keys("zhaoliu003")
        element05 = driver.find_element(By.XPATH, '//button[@id="login_btn" and @class="btn btn-block btn-primary"]')
        element05.click()
        element06 = driver.find_element(By.XPATH,
                                        '//div[@class="form-group has-feedback feedback-left has-error"]/span').text
        if element06 == "密码为空":
            print("注册失败")
            self.book.get_sheet(2).write(7, 6, "passed")
            self.book.get_sheet(2).write(7, 7, "晏凯旋")
            self.book.get_sheet(2).write(7, 8, self.time)
            self.book.save("b.xls")
        else:
            print("注册成功")
            driver.save_screenshot("zhaoliu003--空密码.jpg")
            self.book.get_sheet(2).write(7, 5, "failed")
            self.book.get_sheet(2).write(7, 7, "晏凯旋")
            self.book.get_sheet(2).write(7, 8, self.time)
            self.book.save("b.xls")
        driver.quit()
        assert element06 == "密码为空"
